package project;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import plotterTool.PlotterToolClient;
import ConnectWordsTool.ConnectWordsToolClient;
import Touchy.TouchyClient;
import derivationTool.DerivationToolClient;
import filterCalc.FilterCalcClient;


/**
 * Contains the main()-function of the project. Entry point.
 * 
 * V12 is a singleton class, you get the instance by getInstance(). The
 * constructor is private.
 * 
 * V12 builds the main frame of the application and controls running of
 * learning tools/start panel.
 * 
 * @author Knut Helland and Kristian Sletten
 */
public class V12 extends WindowAdapter
{
	/**
	 * Loads the different tool clients. This is the place to
	 * edit when adding more tools to the project.
	 */
	public void loadTools()
	{
		mTools.add(new PlotterToolClient());
		mTools.add(new ConnectWordsToolClient());
		mTools.add(new TouchyClient());
		mTools.add(new DerivationToolClient());
		mTools.add(new FilterCalcClient());
	}
	
	
	/**
	 * Holds the singleton instance of this V12 object.
	 */
	private static V12 singletonInstance;
	
	/**
	 * The main frame of this application.
	 * 
	 * BorderLayout with:
	 * NORTH: AddressBar
	 * CONTENT: mStartPanel (StartPanel) or mActivePanel (JPanel)
	 */
	JFrame mFrame;
	
	/**
	 * The start panel 
	 */
	StartPanel mStartPanel;
	
	/**
	 * The active panel, which for the most time is
	 * a learning tool's panel.
	 */
	JPanel mActivePanel;
	
	/**
	 * The address bar at the NORTH of main frame.
	 */
	AddressBar mAddressBar = new AddressBar();
	
	/**
	 * Contains a list of all available Learning Tools.
	 */
	ArrayList<ToolClient> mTools = new ArrayList<ToolClient>();
	
	/**
	 * The current active tool.
	 */
	ToolClient mActiveTool = null;
	
	/**
	 * Holds the height of the main frame. (Don't know if this is ever used, 
	 * and the value is better reached with mFrame.getHeight();
	 * @deprecated
	 */
	int mWindowHeight;
	
	/**
	 * Holds the width of the main frame. (Don't know if this is ever used,
	 * and the value is better reached with mFrame.getWidth())
	 * @deprecated
	 */
	int mWindowWidth;
	
	/**
	 * Returns the singleton instance of V12.
	 * @return
	 */
	public static V12 getInstance() {
		if (singletonInstance == null) {
			singletonInstance = new V12();
		}
		
		return singletonInstance;
	}
	
	/**
	 * Main entry point for the whole project. Sets up the main frame.
	 * @param args
	 */
	public static void main(String[] args)
	{
		singletonInstance = V12.getInstance();
		singletonInstance.init();
	}
	

	public void update()
	{
		mFrame.pack();
		mFrame.repaint();
	}
	
	
	/**
	 * Should not be constructed more than once, therefore private.
	 * Use getInstance() instead!
	 */
	private  V12() {}
	
	/**
	 * Sets up the main frame (mFrame).
	 */
	private void init()
	{
		mWindowHeight = 600;
		mWindowWidth = 800;
		
		mFrame = new JFrame();
		
		mFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		mFrame.setTitle("Project V12");
		mFrame.getContentPane().setLayout(new BorderLayout());
		mFrame.addWindowListener(this);
		
		loadTools();
		
		mFrame.add(mAddressBar, BorderLayout.NORTH);
		
		mStartPanel = new StartPanel();
		mActivePanel = mStartPanel;
		
		mFrame.getContentPane().add(mActivePanel, BorderLayout.CENTER);
		mFrame.pack();
		mFrame.setVisible(true);
	}
	

	
	/**
	 * Set active tool by class
	 * @param tool
	 */
	public void setActiveTool(ToolClient toolClient)
	{
		mFrame.getContentPane().remove(mActivePanel);
		mActiveTool = toolClient;
		mActivePanel = toolClient.init();
		mFrame.getContentPane().add(mActivePanel, BorderLayout.CENTER);
		mFrame.pack();
		mFrame.repaint();
		mAddressBar.refresh();
	}
	
	/**
	 * Returns the instance of the active tool's client
	 */
	public ToolClient getActiveTool() {
		return mActiveTool;
	}
	
	
	/**
	 * Returns the ToolClient of the tool number i.
	 * @param i
	 * @return
	 */
	public ToolClient getTool(int i)
	{
		if(mTools.size()<=i)
		{
			return null;
		}
		else
			return mTools.get(i);
	}
	
	/**
	 * Returns a list with all tool clients.
	 * @return
	 */
	public ArrayList<ToolClient> getTools() {
		return mTools;
	}
	
	/**
	 * Quits the active tool and "reactivates" the start panel.
	 */
	public void stopTool()
	{
		if (mActiveTool != null) {		
			mActiveTool.stop();
		}
		mActiveTool = null;
		
		// setter startpanelet tilbake som aktivt panel.
		mFrame.getContentPane().remove(mActivePanel);
		mActivePanel = mStartPanel;
		mFrame.getContentPane().add(mActivePanel, BorderLayout.CENTER);
		mFrame.pack();
		mAddressBar.refresh();
		mFrame.repaint();
	}


	/**
	 * When the main frame is closed: call stop() on active tool in case
	 * it want's to save something etc.
	 */
	@Override
	public void windowClosing(WindowEvent arg0)
	{
		//stops the tool execution, and starts the tool shutdown process before exiting the frame.
		if(mActiveTool != null)
			mActiveTool.stop();
		System.exit(0);
	}
}
